package com.ruoyi.user.service;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUserUtils;
import com.ruoyi.user.domain.UserReceivingWx;
import com.ruoyi.user.mapper.UserReceivingWxMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 收款方式-微信Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-10
 */
@Service
public class UserReceivingWxService {
    @Autowired
    private UserReceivingWxMapper userReceivingWxMapper;

    /**
     * 查询收款方式-微信
     *
     * @param accountId 收款方式-微信主键
     * @return 收款方式-微信
     */
    public UserReceivingWx selectUserReceivingWxByAccountId(Long accountId) {
        return userReceivingWxMapper.selectUserReceivingWxByAccountId(accountId);
    }

    /**
     * 查询收款方式-微信列表
     *
     * @param userReceivingWx 收款方式-微信
     * @return 收款方式-微信
     */
    public List<UserReceivingWx> selectUserReceivingWxList(UserReceivingWx userReceivingWx) {
        return userReceivingWxMapper.selectUserReceivingWxList(userReceivingWx);
    }

    /**
     * 新增收款方式-微信
     *
     * @param userReceivingWx 收款方式-微信
     * @return 结果
     */
    public int insertUserReceivingWx(UserReceivingWx userReceivingWx) {
        userReceivingWx.setCreateTime(DateUtils.getNowDate());
        return userReceivingWxMapper.insertUserReceivingWx(userReceivingWx);
    }

    /**
     * 修改收款方式-微信
     *
     * @param userReceivingWx 收款方式-微信
     * @return 结果
     */
    public int updateUserReceivingWx(UserReceivingWx userReceivingWx) {
        userReceivingWx.setUpdateTime(DateUtils.getNowDate());
        return userReceivingWxMapper.updateUserReceivingWx(userReceivingWx);
    }

    /**
     * 批量删除收款方式-微信
     *
     * @param accountIds 需要删除的收款方式-微信主键
     * @return 结果
     */
    public int deleteUserReceivingWxByAccountIds(Long[] accountIds) {
        long userId = SecurityUserUtils.getUserId();
        // 校验地址是否为当前用户
        List<UserReceivingWx> list = userReceivingWxMapper.selectByIds(Arrays.asList(accountIds));
        boolean hasOtherUser = list.stream().anyMatch(entity -> !entity.getUserId().equals(userId));
        if (hasOtherUser) {
            throw new ServiceException("删除的收款账户不是当前用户的，无法删除");
        }
        return userReceivingWxMapper.deleteUserReceivingWxByAccountIds(accountIds);
    }
}
